package ltd.newbee.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ltd.newbee.mall.api.mall.vo.NewBeeMallSearchGoodsVO;
import ltd.newbee.mall.common.NewBeeMallException;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.PageQuery;
import ltd.newbee.mall.entity.PageResult;
import ltd.newbee.mall.mapper.NewBeeMallGoodsMapper;
import ltd.newbee.mall.service.NewBeeMallGoodsService;
import ltd.newbee.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class NewBeeMallGoodsServiceImpl extends ServiceImpl<NewBeeMallGoodsMapper, NewBeeMallGoods> implements NewBeeMallGoodsService {
    @Autowired
    private NewBeeMallGoodsMapper goodsMapper;

    @Override
    public PageResult<NewBeeMallSearchGoodsVO> searchNewBeeMallGoods(PageQuery pageQuery) {
        String keyword = (String) pageQuery.get("keyword");
        Long goodsCategoryId = (Long) pageQuery.get("goodsCategoryId");
        Integer goodsSellStatus = (Integer) pageQuery.get("goodsSellStatus");
        String orderBy = (String) pageQuery.get("orderBy");

        Page<NewBeeMallGoods> page = new Page<>(pageQuery.getPageIndex(), pageQuery.getPageSize());

        LambdaQueryWrapper<NewBeeMallGoods> queryWrapper = Wrappers.lambdaQuery();

        if (StringUtils.isNotEmpty(keyword)) {
            queryWrapper.and(wrapper -> wrapper
                    .like(NewBeeMallGoods::getGoodsName, keyword)
                    .or()
                    .like(NewBeeMallGoods::getGoodsIntro, keyword)
            );
        }
        if (goodsCategoryId != null) {
            queryWrapper.eq(NewBeeMallGoods::getGoodsCategoryId, goodsCategoryId);
        }
        if (goodsSellStatus != null) {
            queryWrapper.eq(NewBeeMallGoods::getGoodsSellStatus, goodsSellStatus);
        }
        if (StringUtils.isNotEmpty(orderBy)) {
            switch (orderBy) {
                case "new":
                    /*按照发布时间倒序排列*/
                    queryWrapper.orderByDesc(NewBeeMallGoods::getGoodsId);
                    break;
                case "price":
                    /*按照售价从小到大排列*/
                    queryWrapper.orderByAsc(NewBeeMallGoods::getSellingPrice);
                    break;
                default:
                    /*默认按照库存数量从大到小排列*/
                    queryWrapper.orderByDesc(NewBeeMallGoods::getStockNum);
                    break;
            }
        }

        IPage<NewBeeMallGoods> goodsList = goodsMapper.selectPage(page, queryWrapper);

        List<NewBeeMallSearchGoodsVO> newBeeMallSearchGoodsVOS = new ArrayList<>();

        if (!CollectionUtils.isEmpty(goodsList.getRecords())) {
            newBeeMallSearchGoodsVOS = BeanUtil.copyList(goodsList.getRecords(), NewBeeMallSearchGoodsVO.class);
            for (NewBeeMallSearchGoodsVO newBeeMallSearchGoodsVO : newBeeMallSearchGoodsVOS) {
                String goodsName = newBeeMallSearchGoodsVO.getGoodsName();
                String goodsIntro = newBeeMallSearchGoodsVO.getGoodsIntro();
                if (goodsName.length() > 28) {
                    newBeeMallSearchGoodsVO.setGoodsName(goodsName.substring(0, 28) + "...");
                }
                if (goodsIntro.length() > 30) {
                    newBeeMallSearchGoodsVO.setGoodsName(goodsIntro.substring(0, 30) + "...");
                }
            }
        }
        return new PageResult(newBeeMallSearchGoodsVOS, (int) goodsList.getTotal(), (int) goodsList.getSize(), (int) goodsList.getCurrent());
    }

    @Override
    public NewBeeMallGoods getNewBeeMallGoodsById(Long id) {
        LambdaQueryWrapper<NewBeeMallGoods> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(NewBeeMallGoods::getGoodsId, id);
        NewBeeMallGoods newBeeMallGoods = goodsMapper.selectOne(queryWrapper);

        if (newBeeMallGoods == null) {
            NewBeeMallException.fail(ServiceResultEnum.DATA_NOT_EXIST.getResult());
        }
        return newBeeMallGoods;
    }

    @Override
    public List<NewBeeMallGoods> getNewBeeMallGoodsByIds(List<Long> goodsIds) {
        LambdaQueryWrapper<NewBeeMallGoods> goodQueryWrapper = Wrappers.lambdaQuery();
        goodQueryWrapper.in(NewBeeMallGoods::getGoodsId, goodsIds).last("ORDER BY FIELD(goods_id, " + goodsIds.stream().map(String::valueOf).collect(Collectors.joining(",")) + ")");
        return goodsMapper.selectList(goodQueryWrapper);
    }

    @Override
    public boolean batchUpdateGoods(List<NewBeeMallGoods> mewBeeMallGoods) {
        return this.saveOrUpdateBatch(mewBeeMallGoods);
    }
}
